﻿using FluentNHibernate.Mapping;

namespace TaskKernel.Domain.Nhibernate.Mappings
{
    public class TaskMap : ClassMap<Task>
    {
        public TaskMap()
        {
            Table("task_Tasks");
            Id(x => x.UID, "GID").GeneratedBy.GuidNative();
            
            Map(x => x.Priority, "Priority")
               .CustomType<EnumMapper<TaskPriority>>()
               .Not.Nullable();
            Map(x => x.Status, "Status")
                .CustomType<EnumMapper<TaskStatus>>()
                .Not.Nullable();
            Map(x => x.DateCreate, "DateCreate");
            Map(x => x.Code, "ID").Nullable().ReadOnly();
            Map(x => x.DateClose, "DateClose").Nullable();
            Map(x => x.DateExpiration, "DateExpiration").Nullable();
            Map(x => x.PercentComplete, "PercentComplete").Nullable();
            Map(x => x.DateLastHistoryEntry, "DateLastHistoryEntry").Nullable();
            Map(x => x.Tag, "Description").Nullable();
            Map(x => x.TaskName, "TaskName");
            References(x => x.ClosedBy, "ClosedByGID").Nullable();
            References(x => x.AcceptClosedBy, "AcceptCloseByGID").Nullable();
            References(x => x.Owner, "OwnerGID").Nullable();
            References(x => x.Executor, "ExecutorGID").Nullable();
            References(x => x.Department, "DepartmentGID");
            HasMany(x => x.ParticipantsWithOwner)
                .KeyColumns.Add("TaskGID")
                .Inverse()
                .Cascade.AllDeleteOrphan().LazyLoad()
                .AsBag().Access.ReadOnlyPropertyThroughCamelCaseField();
            HasMany(x => x.Readers)
                .KeyColumns.Add("TaskGID")
                .Inverse()
                .Cascade.AllDeleteOrphan().LazyLoad()
                .AsBag().Access.ReadOnlyPropertyThroughCamelCaseField();
            HasMany(x => x.Files)
                .KeyColumns.Add("TaskGID")
                .Inverse()
                .Cascade.AllDeleteOrphan().LazyLoad()
                .AsBag().Access.ReadOnlyPropertyThroughCamelCaseField();
            HasMany(x => x.HistoryEntries)
                .KeyColumns.Add("TaskGID")
                .Inverse()
                .Cascade.AllDeleteOrphan().LazyLoad()
                .AsBag().Access.ReadOnlyPropertyThroughCamelCaseField()
                .OrderBy("Date ASC");
            HasMany(x => x.Reminders)
                .KeyColumns.Add("TaskGID")
                .Inverse()
                .Cascade.AllDeleteOrphan().LazyLoad()
                .AsBag().Access.ReadOnlyPropertyThroughCamelCaseField();
            HasMany(x => x.WorkingPeriods)
                .KeyColumns.Add("TaskGID")
                .Inverse()
                .Cascade.AllDeleteOrphan().LazyLoad()
                .AsBag().Access.ReadOnlyPropertyThroughCamelCaseField();
            HasMany(x => x.Ratings)
                .KeyColumns.Add("TaskGID")
                .Inverse()
                .Cascade.AllDeleteOrphan().LazyLoad()
                .AsBag().Access.ReadOnlyPropertyThroughCamelCaseField();
        }
    }
}